//===============================================================================
// Microsoft patterns & practices
// Composite Application Guidance for Windows Presentation Foundation
//===============================================================================
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Windows;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;
using P2.Cuberry.Framework.Helper;


namespace P2.Cuberry.Modules.Framework.UserGroup
{
    public class UserGroupsPresenter
    {
        private IUnityContainer container;
        private IRegionManager regionManager;
        private IUserGroupsListPresenter listPresenter;
        //private IUserGroupsController UserGroupController;

        public UserGroupsPresenter(IUserGroupsView view, IUserGroupsListPresenter listPresenter,  IUnityContainer container, 
            IRegionManager regionManager)
        {
            this.container = container;
            this.regionManager = regionManager;
            this.View = view;
            this.listPresenter = listPresenter;
            this.listPresenter.UserGroupSelected += new EventHandler<DataEventArgs<BusinessEntities.UserGroup>>(this.OnUserGroupSelected);
            this.listPresenter.UserGroupOpened += new EventHandler<DataEventArgs<BusinessEntities.UserGroup>>(this.OnUserGroupOpened);         

            View.SetHeader(listPresenter.View);
        }

        public IUserGroupsView View { get; set; }


        public void OnUserGroupSelected(object sender, DataEventArgs<BusinessEntities.UserGroup> e)
        {
  
        }
        

        public void OnUserGroupOpened(object sender, DataEventArgs<BusinessEntities.UserGroup> e)
        {
            try
            {
                IUserGroupsDetailsView dv = this.container.Resolve<IUserGroupsDetailsView>();
                e.Value.FillMyObject();

                Services.IUserGroupService service = this.container.Resolve<Services.IUserGroupService>();
                UserGroupsDetailsPresentationModel pm = new UserGroupsDetailsPresentationModel(dv, e.Value, service, container, regionManager, sender);
                pm.ParentList = listPresenter.View.Model.Model;
                dv.FlowDirection = (P2Translator.Culture.TextInfo.IsRightToLeft ? FlowDirection.RightToLeft : FlowDirection.LeftToRight);
                dv.ShowDialog();
                pm = null;
                dv = null;
                GC.Collect();
            }
            catch (System.Exception ex) //AM:TC
            {
                P2ExceptionHandler.LogException(ex, string.Format("{0}.{1}", this.GetType().Name, System.Reflection.MethodInfo.GetCurrentMethod().Name));
            }
        }
       

        public void OnUserGroupFind(object sender, DataEventArgs<BusinessEntities.UserGroupQuery> e)
        {
            if (listPresenter != null)
            {
                listPresenter.FindUserGroup(e.Value);
            }
        }

    }
}
